package com.ruicar.afs.cloud.afscase.externalinterface.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.common.utils.DicUtils;
import com.ruicar.afs.cloud.afscase.externalinterface.condition.UsedCarCondition;
import com.ruicar.afs.cloud.afscase.externalinterface.mapper.ExternalUsedCarMapper;
import com.ruicar.afs.cloud.afscase.externalinterface.service.ExternalUsedCarService;
import com.ruicar.afs.cloud.afscase.externalinterface.vo.ExternalUsedCarVo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ContractStatusEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.CustRoleEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.FlagTypeEnum;
import com.ruicar.afs.cloud.components.datadicsync.DicHelper;
import com.ruicar.afs.cloud.components.datadicsync.dto.DicDataDto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jiaqi.guo
 * @version 1.0
 * @description: 二手车历史存量vin查询
 * @created 2020/7/21 15:49
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class ExternalUsedCarServiceImpl extends ServiceImpl<ExternalUsedCarMapper, ExternalUsedCarVo> implements ExternalUsedCarService {
    private CaseContractInfoService caseContractInfoService;

    /**
     * 根据vin查历史存量
     */
    public List<ExternalUsedCarVo> getUsedCarhistory(String vin) {
        UsedCarCondition carCondition = new UsedCarCondition();
        carCondition.setVin(vin);
        carCondition.setCustRole(CustRoleEnum.MIANCUST.getCode());
        List<ExternalUsedCarVo> carVoList = new ArrayList<>();
        List<ExternalUsedCarVo> list = this.baseMapper.getUsedCarhistory(carCondition);
        //业务状态字典
        Map<String, List<DicDataDto>> dicMap = DicHelper.getDicMaps("businessStateIn");
        if (CollectionUtils.isNotEmpty(list)) {
            for (ExternalUsedCarVo usedCarVo : list) {
                //默认多品
                String businessStateIn = DicUtils.translate(usedCarVo.getStatus(), dicMap);
                usedCarVo.setStatusName(businessStateIn);
                List<CaseContractInfo> caseContractInfos = caseContractInfoService.list(Wrappers.<CaseContractInfo>query().lambda()
                        .eq(CaseContractInfo::getApplyNo, usedCarVo.getApplicationNo()));
                if (CollectionUtils.isNotEmpty(caseContractInfos)) {
                    if (caseContractInfos.get(0).getContractStatus() != null) {
                        //贷偿中
                        if (caseContractInfos.get(0).getContractStatus().equals(ContractStatusEnum.contractEffective.toString())) {
                            usedCarVo.setFlagName(FlagTypeEnum.CREDIT_CLAIMS.getDesc());
                            usedCarVo.setFlag(Long.parseLong(FlagTypeEnum.CREDIT_CLAIMS.getCode()));
                        } else {
                            usedCarVo.setFlagName(FlagTypeEnum.APPLICATION.getDesc());
                            usedCarVo.setFlag(Long.parseLong(FlagTypeEnum.APPLICATION.getCode()));
                        }
                    } else {
                        usedCarVo.setFlagName(FlagTypeEnum.APPLICATION.getDesc());
                        usedCarVo.setFlag(Long.parseLong(FlagTypeEnum.APPLICATION.getCode()));


                    }
                } else {
                    usedCarVo.setFlagName(FlagTypeEnum.APPLICATION.getDesc());
                    usedCarVo.setFlag(Long.parseLong(FlagTypeEnum.APPLICATION.getCode()));
                }
                carVoList.add(usedCarVo);
            }
            log.info("根据vin查历史存量返回结果" + carVoList.size());
        } else {
            log.info("根据vin查历史存量无返回结果");
        }
        return carVoList;
    }
}
